import React, {useEffect, useState} from "react";
import axios from 'axios';
import {View} from "native-base";
import side from '../../capsulation/sider';
import {Dimensions, FlatList, StyleSheet} from "react-native";
import {BeerPreviewCard} from "./BeerPreviewCard";
import EmptyMsg from "../../commponents/sealCutting/emptyMsg";
import {SearchIntroduce} from "./searchIntroduce";
const {height} = Dimensions.get('window');
export const CommonSearch = ({tempIpt, navigation, time}) => {
    const [laiyuan, setLaiyuan]=useState([]);
    const [length, setLength]=useState(1);
    const [name, setName]=useState([]);
    const [niandai, setNiandai]=useState([]);
    const [photo1, setPhoto1]=useState([]);
    const [photo2, setPhoto2]=useState([]);
    const [photo3, setPhoto3]=useState([]);
    async function searchMsg(val,time=''){
        let data = await axios({
            url: `https://www.guyinbaike.com/apiSearch?search_word=${side.getSider(val)}&search_age=${time}`,
            data: {
                key: false
            }
        })
        setLaiyuan(String(tempIpt).trim().length!==0&&data.data.laiyuan);
        setName(String(tempIpt).trim().length!==0&&data.data.name);
        setNiandai(String(tempIpt).trim().length!==0&&data.data.niandai);
        setPhoto1(String(tempIpt).trim().length!==0&&data.data.photo1);
        setPhoto2(String(tempIpt).trim().length!==0&&data.data.photo2);
        setPhoto3(String(tempIpt).trim().length!==0&&data.data.photo3);
        setLength(String(tempIpt).trim().length!==0&&data.data.length);
    }
    useEffect(()=>{
        if(time === '全部')time = '';
        searchMsg(tempIpt, time);
    },[tempIpt,time])
    function _handleLoadMore(){
        //
    }
    return <View style={styles.velless}>
        <FlatList
            data={laiyuan}
            ListHeaderComponent={<SearchIntroduce tempIpt={tempIpt} time={time} length={length}/>}
            renderItem={({item,index}) => <BeerPreviewCard navigation={navigation} name={name[index]} laiyuan={item} niandai={niandai[index]} photo1={photo1[index]} photo2={photo2[index]}/>}/>
        {length===0&&<EmptyMsg/>}
    </View>
}

const styles = StyleSheet.create({
    velless: {
        height: height - 60 - 60 - 1 - 80 - 60 - 40,
        paddingBottom: 10
    }
})